package com.distributed.kv.dao.mapper;

import com.distributed.kv.entity.KvPair;
import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

/**
 * KV数据访问接口
 * 
 * 基于Spring Data Cassandra,提供基础CRUD操作
 * 支持批量操作和条件查询
 */
@Repository
public interface KvRepository extends CassandraRepository<KvPair, String> {
    
    /**
     * 根据ID查找KV对
     */
    @Query("SELECT * FROM kv_pairs WHERE id = :id ALLOW FILTERING")
    Optional<KvPair> findById(@Param("id") Long id);
    
    /**
     * 批量根据key查找
     */
    @Query("SELECT * FROM kv_pairs WHERE key IN :keys")
    List<KvPair> findByKeyIn(@Param("keys") List<String> keys);
    
    /**
     * 查找访问次数最高的热点数据
     */
    @Query("SELECT * FROM kv_pairs WHERE access_count > :minCount ALLOW FILTERING")
    List<KvPair> findHotKeys(@Param("minCount") Long minCount);
    
    /**
     * 根据创建时间范围查询
     */
    @Query("SELECT * FROM kv_pairs WHERE created_at >= :startTime AND created_at <= :endTime ALLOW FILTERING")
    List<KvPair> findByCreatedAtBetween(@Param("startTime") Instant startTime, @Param("endTime") Instant endTime);
    
    /**
     * 更新访问计数
     */
    @Query("UPDATE kv_pairs SET access_count = access_count + 1, updated_at = :now WHERE key = :key")
    void incrementAccessCount(@Param("key") String key, @Param("now") Instant now);
    
    /**
     * 批量插入(使用CQL批处理)
     */
    @Query("BEGIN BATCH " +
           "INSERT INTO kv_pairs (key, id, value, created_at, updated_at, access_count) " +
           "VALUES (:key, :id, :value, :createdAt, :updatedAt, :accessCount) " +
           "APPLY BATCH")
    void batchInsert(@Param("key") String key, 
                    @Param("id") Long id,
                    @Param("value") String value,
                    @Param("createdAt") Instant createdAt,
                    @Param("updatedAt") Instant updatedAt,
                    @Param("accessCount") Long accessCount);
    
    /**
     * 按值分组统计访问次数
     */
    @Query("SELECT value, SUM(access_count) as total_access_count, COUNT(*) as record_count " +
           "FROM kv_pairs WHERE value LIKE :valuePattern " +
           "GROUP BY value ALLOW FILTERING")
    List<Object[]> groupByValue(@Param("valuePattern") String valuePattern);
    
    /**
     * 按日期分组统计访问次数
     */
    @Query("SELECT * FROM kv_pairs WHERE created_at >= :startTime AND created_at <= :endTime ALLOW FILTERING")
    List<KvPair> groupByDate(@Param("startTime") Instant startTime, @Param("endTime") Instant endTime);
    
    /**
     * 按日期和值分组统计访问次数
     */
    @Query("SELECT toDate(created_at) as date, value, SUM(access_count) as total_access_count, COUNT(*) as record_count " +
           "FROM kv_pairs WHERE value LIKE :valuePattern " +
           "AND created_at >= :startTime AND created_at <= :endTime " +
           "GROUP BY toDate(created_at), value ALLOW FILTERING")
    List<Object[]> groupByDateAndValue(@Param("valuePattern") String valuePattern, 
                                      @Param("startTime") Instant startTime, 
                                      @Param("endTime") Instant endTime);
    
    /**
     * 查找包含特定值的记录
     */
    @Query("SELECT * FROM kv_pairs WHERE value LIKE :valuePattern ALLOW FILTERING")
    List<KvPair> findByValueContaining(@Param("valuePattern") String valuePattern);
}